import * as fs from 'fs';
import * as path from 'path';
import * as electron from 'electron'

// ES6 Module
import * as menu from './menu';
import { editorHistory, TestCommand, AbstractCommand } from './history';
import { AbstractPropertyItemRenderer, createPropertyItemRenderer } from './propertyitemrenderer';
import { project } from './project';
import { changeDataInspector, getInspector, DataInspector } from './dataInspector';
import { initPreviewBoard, openPreviewPanel, openMapEditor, openPreviewDebugPanel } from './mainboard';
import { currentData } from './currentdata';

menu.run();


initPreviewBoard();

export interface PropertyMetadata {

    key: string,
    description: string,
    type: "string" | 'number' | "dropdown" | "primarykey" | "image",
    default: any;
    options?: {
        filename: string,
        prefix: string
    }
}


export class ModifyPropertyCommand extends AbstractCommand {


    constructor(private obj: any, private to: any, private from: any, private key: string, private inspector: DataInspector, private renderer: AbstractPropertyItemRenderer) {
        super();
    }


    execute(): void {
        this.obj[this.key] = this.to;
        this.inspector.updateItem(this.obj);
    }
    revert(): void {
        this.obj[this.key] = this.from;
        this.renderer.updateView(this.from);
    }
}

export interface DataMetadata {
    filename: string;
    prefix: string;
    title: string;
    propertyMetadatas: PropertyMetadata[]
}

const missionMetaData: DataMetadata = {

    filename: "mission.json",
    title: "任务编辑器",
    prefix: "tasks",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "primarykey"
        },
        {
            key: 'title',
            description: "任务标题",
            default: "新的任务",
            type: "string"
        },
        {
            key: 'needLevel',
            description: "接受任务需要的等级",
            default: 1,
            type: "number"
        },
        {
            key: 'fromNpcId',
            description: "接受任务的NPCID",
            default: 1,
            type: "dropdown",
            options: {
                filename: 'npc.json',
                prefix: "npc"

            }
        },
        {
            key: 'toNpcId',
            description: "提交任务的NPCID",
            default: 1,
            type: "dropdown",
            options: {
                filename: 'npc.json',
                prefix: "npc"

            }
        }
    ]
}


const npcMetadata: DataMetadata = {
    filename: "npc.json",
    prefix: 'npc',
    title: "NPC编辑器",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "primarykey"
        },
        {
            key: 'name',
            description: "NPC姓名",
            type: "string",
            default: "无名氏"
        },
        {
            key: 'image',
            description: "NPC头像",
            type: "image",
            default: ""
        },
        {
            key: "avatar",
            description: "NPC模型",
            type: "image",
            default: ""
        }
    ]
}

const mapMetadata: DataMetadata = {
    filename: "map.json",
    prefix: 'map',
    title: "地图编辑器",
    propertyMetadatas: [
    ]
}


const metadatas: DataMetadata[] = [
    missionMetaData,
    npcMetadata,
    mapMetadata
]


const buttonGroup = document.getElementById("buttonGroup");
if (buttonGroup) {
    for (let metadata of metadatas) {
        const button = document.createElement("button");
        button.innerHTML = metadata.title;
        buttonGroup.appendChild(button);
        button.onclick = () => {
            currentData.setCurrentMetaData(metadata);
            if (metadata.prefix == "map") {
                openMapEditor();
                changeDataInspector(null);
            }
            else {
                openPreviewPanel(true);
                changeDataInspector(metadata)
            }

        }
    }
}

export function save() {
    currentData.saveAndReload();
}



const openDeubgPanelButton = document.getElementById("openDebugPanelButton") as HTMLButtonElement;
openDeubgPanelButton.onclick = () => {

    openPreviewDebugPanel();


}


const refreshButton = document.getElementById("refreshButton") as HTMLButtonElement;
refreshButton.onclick = () => {

}

openPreviewPanel(false);